from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView, TemplateView

from main.forms import ArticleForm
from main.models import ArticleModel, CatagoryForm


# Create your views here.
class IndexView(TemplateView):
    template_name = 'main/index.html'
    extra_context = {
        'catagorys':CatagoryForm.objects.filter(parent__isnull=True),
    }


class AritcleModleListView(ListView):
    def get_queryset(self):
       return  ArticleModel.objects.filter(author=self.request.user)
    paginate_by = 2





class ArticleModleCreateView(CreateView):
    success_url = '/'
    model = ArticleModel
    form_class = ArticleForm
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)
class ArticleModleUpdateView(UpdateView):
    def get_success_url(self):
        # 更新成功后重定向到该文章的详情页面
        return reverse_lazy('user:detail', kwargs={'pk': self.object.pk})

    model = ArticleModel
    form_class = ArticleForm

class ArticleManagerIndexView(ListView):
    template_name = 'main/index.html'


    def get_queryset(self):
        return ArticleModel.objects.all()
